package zjitc.web.servlet;

import zjitc.domain.Test;
import zjitc.service.UserService;
import zjitc.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/registerServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 设置编码
        request.setCharacterEncoding("utf-8");
        //2. 获取数据
        Test user = new Test();
        user.setUsername(request.getParameter("username"));
        user.setSex(request.getParameter("sex"));
        user.setAge(Integer.parseInt(request.getParameter("age")));
        user.setName(request.getParameter("name"));
        user.setPassword(request.getParameter("password"));
        //3. 调用service存储数据
        UserService service = new UserServiceImpl();
        service.registerUser(user);

        //4. 跳转到登录页面
        request.getRequestDispatcher("/index.jsp").forward(request,response);

    }
}
